package com.example.gzysmartcity.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.ThreadUtils;
import com.example.gzysmartcity.repository.model.Line;
import com.example.gzysmartcity.repository.model.LineStep;
import com.example.gzysmartcity.repository.model.MyLine;
import com.example.gzysmartcity.repository.model.Process;
import com.example.gzysmartcity.repository.model.Supply;
import com.example.gzysmartcity.repository.model.SupplyList;
import com.example.gzysmartcity.repository.model.SupplyNum;
import com.example.gzysmartcity.repository.remote.State;
import com.example.gzysmartcity.utils.NetworkUtil;

import java.util.ArrayList;
import java.util.List;

public class FactoryViewModel extends ViewModel {
    MutableLiveData<State> factoryLiveData=new MutableLiveData<>();
    MutableLiveData<State> factoryLiveDat1=new MutableLiveData<>();
    List<Supply.DataBean> supplyList;
    List<SupplyList.DataBean> supplylistList;
    List<SupplyNum> mSupplyNums;
    List<Process.DataBean> ProcessList;
    List<Line.DataBean> LineList;
    List<LineStep.DataBean> LineStepList;
    List<MyLine> MyLineList;

    public void getSupplyData(){
        ThreadUtils.executeByCached(new ThreadUtils.Task<Supply>() {
            @Override
            public Supply doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("Supply/getAll?", null, Supply.class, 0);
            }

            @Override
            public void onSuccess(Supply result) {
                supplyList=result.getData();
                getSupplyListData();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {

            }
        });
    }

    public void getSupplyListData(){
        ThreadUtils.executeByCached(new ThreadUtils.Task<SupplyList>() {
            @Override
            public SupplyList doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("SupplyList/getAll?", null, SupplyList.class, 0);
            }

            @Override
            public void onSuccess(SupplyList result) {
                supplylistList=result.getData();
                getSupplyNumData();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {

            }
        });
    }

    public void getSupplyNumData(){
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Object>() {
            @Override
            public Object doInBackground() throws Throwable {
                mSupplyNums=new ArrayList<>();
                for (Supply.DataBean s: supplyList){
                    mSupplyNums.add(new SupplyNum(s.getSupplyName(),supplylistList.stream().filter(a->a.getSupplyId()==s.getId()).mapToInt(SupplyList.DataBean::getNum).sum()));
                }
                return null;
            }
            @Override
            public void onSuccess(Object result) {
                factoryLiveData.postValue(State.SUCCESS);
            }
        });
    }

    public int getSupplyNumAll(){
        return supplylistList.stream().mapToInt(SupplyList.DataBean::getNum).sum();
    }

    public void getLineData(){
        //生产线
        ThreadUtils.executeByCached(new ThreadUtils.Task<Line>() {
            @Override
            public Line doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("Line/getAll?", null, Line.class, 0);
            }

            @Override
            public void onSuccess(Line result) {
                LineList=result.getData();
                getLineStepData();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {

            }
        });
    }

    public void getLineStepData(){
        //生产线环节
        ThreadUtils.executeByCached(new ThreadUtils.Task<LineStep>() {
            @Override
            public LineStep doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("LineStep/getAll?", null, LineStep.class, 0);
            }

            @Override
            public void onSuccess(LineStep result) {
                LineStepList=result.getData();
                getProcessData();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {

            }
        });
    }

    public void getProcessData(){
        //工序环节
        ThreadUtils.executeByCached(new ThreadUtils.Task<Process>() {
            @Override
            public Process doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("Process/getAll?", null, Process.class, 0);
            }
        
            @Override
            public void onSuccess(Process result) {
                ProcessList=result.getData();
                getMyLineData();
            }
        
            @Override
            public void onCancel() {
                
            }
        
            @Override
            public void onFail(Throwable t) {
                
            }
        });
    }

    public void getMyLineData(){
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Object>() {
            @Override
            public Object doInBackground() throws Throwable {
                MyLineList=new ArrayList<>();
                for (LineStep.DataBean lineStep:LineStepList){
                    String lineName=LineList.stream().filter(a->a.getId()==lineStep.getLineId()).findFirst().get().getLineName();
                    String process=ProcessList.stream().filter(a->a.getId()==lineStep.getProcessId()).findFirst().get().getProcessName();
                    String processContent=ProcessList.stream().filter(a->a.getId()==lineStep.getProcessId()).findFirst().get().getContent();
                    MyLineList.add(new MyLine(lineStep.getIconUrl(),lineName,lineStep.getLineStepName(),process,processContent,lineStep.getCostTime(),lineStep.getStep()
                    +"",lineStep.getHp()+"",lineStep.getConsume()+""));
                }
                return null;
            }

            @Override
            public void onSuccess(Object result) {
                factoryLiveDat1.postValue(State.SUCCESS);
            }
        });
    }
    public List<Object> getBannerImageFactory(List<MyLine> myLines){
        List<Object> imageStr=new ArrayList<>();
        for (MyLine myLine:myLines) {
            imageStr.add(myLine.getPicorNames());
        }
        return imageStr;
    }
    
    public MutableLiveData<State> getFactoryLiveData() {
        return factoryLiveData;
    }

    public List<Supply.DataBean> getSupplyList() {
        return supplyList;
    }

    public List<Process.DataBean> getProcessList() {
        return ProcessList;
    }

    public List<Line.DataBean> getLineList() {
        return LineList;
    }

    public List<LineStep.DataBean> getLineStepList() {
        return LineStepList;
    }

    public List<MyLine> getMyLineList() {
        return MyLineList;
    }

    public List<Supply.DataBean> getSupplysList() {
        return supplyList;
    }

    public List<SupplyList.DataBean> getSupplylistList() {
        return supplylistList;
    }

    public List<SupplyNum> getSupplyNums() {
        return mSupplyNums;
    }

    public MutableLiveData<State> getFactoryLiveDat1() {
        return factoryLiveDat1;
    }
}
